package com.srwl.mytx.utils;


import androidx.annotation.NonNull;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class DateUtil {

    /**
     * @param startDate 起始日期
     * @param timeout   有效期
     * @param unit
     * @return
     */
    public static boolean isExpired(Date startDate, long timeout, TimeUnit unit) {

        if (startDate == null) {
            return true;
        }
        long startDateTime = startDate.getTime();
        long currentDateTime = new Date().getTime();

        long millis = unit.toMillis(timeout);

        return currentDateTime - startDateTime > millis;
    }

    public static String getTimeSpanWithMillis(Date startDate, Date endDate) {


        long startDateTime = startDate.getTime();
        long endDateTime = endDate.getTime();
        long diff = endDateTime - startDateTime;
        //  return TimeUnit.MILLISECONDS.toMillis(endDateTime - startDateTime);
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        long second = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
        if (days != 0) {
            return +days + "天" + hours + "小时" + minutes + "分";
        } else if (hours != 0) {
            return +hours + "小时" + minutes + "分";
        } else if (minutes != 0) {
            return +minutes + "分" + second + "秒";
        } else {
            return +second + "秒";
        }
    }

    public static String computeDurationTime(Date startDate, Date endDate) {
        long startDateTime = startDate.getTime();
        long endDateTime = endDate.getTime();
        long diff = endDateTime - startDateTime;
        return computeDurationTime(diff);
    }

    @NonNull
    public static String computeDurationTime(long milliSeconds) {
        long days = milliSeconds / (1000 * 60 * 60 * 24);
        long hours = (milliSeconds - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (milliSeconds - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        long second = (milliSeconds - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
        String duration = "";
        if (days != 0) {
            duration += days + "天";
        }
        if (hours != 0) {
            duration += hours + "小时";
        }
        if (minutes != 0) {
            duration += minutes + "分钟";
        }
        if (second != 0) {
            duration += second + "秒";
        }
        return duration;
    }


}
